import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.LinkedList;
import javax.imageio.ImageIO;


// Project: 

/******************************************************************************
 * <p>
 * File: heroControl.java
 * </p>
 * @author  Tadhg O'Sullivan, rogue, Goncalo Bastos
 * @author <a href="mailto:tadhg.osullivan@ucdconnect.ie">tadhg.osullivan@ucdconnect.ie</a>
 * @version 1.0, 2007
 * @created 2-Mar-07 8:01:40 PM
 * @revised 
 * Copyright 2007 Tadhg O'Sullivan
 * <p>
 * Description: assigns coordinates to the hero for movement
 * </p>
 *  
 *  This file is part of Retribution.
 *
 *  Retribution is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Retribution is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Retribution; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *   
 *****************************************************************************/

public class heroControl {
	protected BufferedImage imgHero = null;
	/**
	 * 
	 * @param h A hero sprite to be moved
	 * @param mt An array filled with map tile to control collision
	 */
	public void moveHero(LinkedList<enemy> el, hero h, mapTile[][] mt, int mapHeigth){
		String direction = h.getMovement();
		BufferedImage tile2;
		Image scaledImage;
		Interaction inter = new Interaction();
		int x = el.size();
		//int countKills = 0;
		for(int i = 0; i<x;i++){
			if(el.get(i).getLifeState()==true){
				if(direction.equalsIgnoreCase("down")){
					if(el.get(i).getX()==h.getX() && el.get(i).getY()==h.getY()+(8*(mapHeigth/200))){
						direction = "";
						inter.heroAttack(h, el.get(i));
						h.setMovedState(true);
					}
				}else if(direction.equalsIgnoreCase("up")){
					if(el.get(i).getX()==h.getX() && el.get(i).getY()==h.getY()-(8*(mapHeigth/200))){
						direction = "";
						inter.heroAttack(h, el.get(i));
						h.setMovedState(true);
					}
				}else if(direction.equalsIgnoreCase("left")){
					if(el.get(i).getX()==h.getX()-(8*(mapHeigth/200)) && el.get(i).getY()==h.getY()){
						direction = "";
						inter.heroAttack(h, el.get(i));
						h.setMovedState(true);
					}
				}else if(direction.equalsIgnoreCase("right")){
					if(el.get(i).getX()==h.getX()+(8*(mapHeigth/200)) && el.get(i).getY()==h.getY()){
						direction = "";
						inter.heroAttack(h, el.get(i));
						h.setMovedState(true);
					}
				}
			}
		}
		if(direction.equalsIgnoreCase("down")){
			// check for map collision
			if(((((h.getY())/ (8*(mapHeigth/200)))) == 24) || ((mt[(h.getY() + (8*(mapHeigth/200)))/ (8*(mapHeigth/200))][h.getX() / (8*(mapHeigth/200))].collisionState == true))){
				h.setMoved();
			}else{
						try {
							// get the tileset BufferedImage
							imgHero = ImageIO.read(this.getClass().getResource("herofr.gif"));
						} catch (IOException e) {
						}
						tile2 = imgHero;
						scaledImage = tile2.getScaledInstance(8*((mapHeigth + (mapHeigth / 2) + ((mapHeigth / 100 ) * 10))/320),8 * (mapHeigth/200), tile2.SCALE_FAST);	
						h.setImage(scaledImage);
						h.setX(h.getX());
						h.setY(h.getY() + (8*(mapHeigth/200)));
						h.setMoved();
			}
			h.setMovedState(true);
		}
		else if(direction.equalsIgnoreCase("up")){
			// check for map collision
			if(((((h.getY())/ (8*(mapHeigth/200)))) == 0) || ((mt[(h.getY() - (8*(mapHeigth/200))) / (8*(mapHeigth/200))][h.getX() / (8*(mapHeigth/200))].collisionState == true))){
				h.setMoved();
			}else{
						try {
							// get the tileset BufferedImage
							imgHero = ImageIO.read(this.getClass().getResource("herobk.gif"));
						} catch (IOException e) {
						}
						tile2 = imgHero;
						scaledImage = tile2.getScaledInstance(8*((mapHeigth + (mapHeigth / 2) + ((mapHeigth / 100 ) * 10))/320),8 * (mapHeigth/200), tile2.SCALE_FAST);
				
						h.setImage(scaledImage);
						h.setX(h.getX());
						h.setY(h.getY() - (8*(mapHeigth/200)));
						h.setMoved();
			}
			h.setMovedState(true);
		}
		else if(direction.equalsIgnoreCase("left")){
			// check for map collision
			if(((((h.getX())/ (8*(mapHeigth/200)))) == 0) || ((mt[h.getY() / (8*(mapHeigth/200))][(h.getX() - (8*(mapHeigth/200)))/ (8*(mapHeigth/200))].collisionState == true))){
				h.setMoved();
			}else{
				try {
						// get the tileset BufferedImage
						imgHero = ImageIO.read(this.getClass().getResource("herolt.gif"));
					} catch (IOException e) {
					}
					tile2 = imgHero;
					scaledImage = tile2.getScaledInstance(8*((mapHeigth + (mapHeigth / 2) + ((mapHeigth / 100 ) * 10))/320),8 * (mapHeigth/200), tile2.SCALE_FAST);
				
					h.setImage(scaledImage);
					h.setX(h.getX() - (8*(mapHeigth/200)));
					h.setY(h.getY());
					h.setMoved();
			}
			h.setMovedState(true);
		}
		else if(direction.equalsIgnoreCase("right")){
			// check for map collision
			if(((((h.getX())/ (8*(mapHeigth/200)))) == 39) || ((mt[h.getY() / (8*(mapHeigth/200))][(h.getX() + (8*(mapHeigth/200)))/ (8*(mapHeigth/200))].collisionState == true))){
				h.setMoved();
			}else{
					try {
							// get the tileset BufferedImage
							imgHero = ImageIO.read(this.getClass().getResource("herort.gif"));
						} catch (IOException e) {
						}
						tile2 = imgHero;
						scaledImage = tile2.getScaledInstance(8*((mapHeigth + (mapHeigth / 2) + ((mapHeigth / 100 ) * 10))/320),8 * (mapHeigth/200), tile2.SCALE_FAST);
						
						h.setImage(scaledImage);
						h.setX(h.getX() + (8*(mapHeigth/200)));
						h.setY(h.getY());
						h.setMoved();
			}
			h.setMovedState(true);
		}
	}
}
